package com.bwie.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupJoin;
import com.bwie.mapper.GroupJoinMapper;
import com.bwie.service.GroupJoinService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GroupJoinServiceImpl extends ServiceImpl<GroupJoinMapper, GroupJoin> implements GroupJoinService{
    @Autowired
    private GroupJoinMapper groupJoinMapper;
    @Override
    public R getGroupInfoList(PageInfoVo pageInfoVo) {
        Page<GroupJoin> groupJoinPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<GroupJoin> groupJoinList = groupJoinMapper.selectGroupInfoList(groupJoinPage,pageInfoVo);
        for (GroupJoin record : groupJoinList.getRecords()) {
            String joinImages = record.getJoinImages();
            if(StringUtils.isNotEmpty(joinImages)){
                List<String> strings = JSON.parseArray(joinImages, String.class);
                record.setImagesList(strings);
            }
        }
        return R.OK(groupJoinList);
    }

    @Override
    public R addGroupInfo(GroupJoin groupJoin) {
        GroupJoin groupJoin1 = groupJoinMapper.selectOne(
                new QueryWrapper<GroupJoin>().lambda().eq(GroupJoin::getJoinName, groupJoin.getJoinName())
        );
        if(ObjectUtils.isNotEmpty(groupJoin1)){
            return R.ERROR(500,"拼团活动已存在,请不要重复参团");
        }
        List<String> getGroupTime = groupJoin.getGetGroupTime();
        groupJoin.setBeginTime(getGroupTime.get(0));
        groupJoin.setEndTime(getGroupTime.get(1));
        groupJoinMapper.insert(groupJoin);
        return R.OK();
    }

    @Override
    public R updateGroupInfo(GroupJoin groupJoin) {
        groupJoinMapper.updateById(groupJoin);
        return R.OK();
    }

    @Override
    public R deleteGroupJoin(Integer joinId) {
        groupJoinMapper.deleteById(joinId);
        return R.OK();
    }
}
